﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using STOCK.DAL;
using STOCK.Model;
using System.Data.SqlClient;
using System.Data;

namespace STOCK.DAL
{
     public class T_FunctionDAL
    {
         public void InsertByCatId(T_Function fuction, Guid CatId)
         {
             SqlHelper.ExecuteNonQuery(@"Insert into T_Function (Id,CatId,Name,Fun,Discription) values(newid(),@CatId,@Name,@Fun,@Discription)",
                 new SqlParameter("@CatId", CatId),
                 new SqlParameter("@Name", fuction.Name),
                 new SqlParameter("@Fun", fuction.Fun),
                 new SqlParameter("@Discription", fuction.Discription));
         }
         public T_Function[] GetByCatId(Guid CatId)
         {
             DataTable dt = SqlHelper.ExecuteDataTable("select * from T_Function where CatId=@CatId order by Fun",
                 new SqlParameter("@CatId",CatId));
             return ToT_Function(dt);
         }
         public T_Function GetById(Guid id)
         {
             DataTable dt = SqlHelper.ExecuteDataTable("select * from T_Function where Id=@Id",
                 new SqlParameter("@Id", id));
             if (dt.Rows.Count == 1)
             {
                 return ToModel(dt.Rows[0]);
             }
             else
             {
                 throw new Exception("存在重复Id");
             }
         }
         public T_Function ToModel(DataRow row)
         {
             T_Function fun = new T_Function();
             fun.Id = (Guid)row["Id"];
             fun.CatId = (Guid)row["CatId"];
             fun.Name = (string)row["Name"];
             fun.Fun = (string)row["Fun"];
             fun.Discription = (string)row["Discription"];
             return fun;
         }

         private T_Function[] ToT_Function(DataTable dt)
         {
             T_Function[] items = new T_Function[dt.Rows.Count];
             for (int i = 0; i < dt.Rows.Count; i++)
             {
                 T_Function item = new T_Function();
                 item = ToModel(dt.Rows[i]);
                 items[i] = item;
             }
             return items;
         }
    }
}
